﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using VMuktiAPI;

namespace VMuktiGrid.CustomMenu
{
    /// <summary>
    /// Interaction logic for comboBox.xaml
    /// </summary>
    public partial class ctlMenu : UserControl
    {
        ctlMenuItem _objMISave = null;
        bool _ShowSaveOption = false;

        public bool ShowSaveOption
        {
            get { return _ShowSaveOption; }
            set 
            { 
                _ShowSaveOption = value;
                if (!value)
                {
                    stPanel.Children.Remove(_objMISave);
                }
                else
                {
                    stPanel.Children.Insert(4, _objMISave);
                }
            }
        }

        public ctlMenu()
        {
            try
            {
                InitializeComponent();
                _objMISave = objMISave;
                stPanel.Children.Remove(_objMISave);
            }
            catch (Exception ex)
            {
                ex.Data.Add("My Key", "ctlMenu()--:--ctlMenu.xaml.cs--:--" + ex.Message + " :--:--");
                ClsException.LogError(ex);
                ClsException.WriteToErrorLogFile(ex);
            }
        }

        public void AddItem(string strBuddyName)
        {
            try
            {
                ctlMenuItem objMenuItem = new ctlMenuItem();
                objMenuItem.Title = strBuddyName;
                stPanel.Children.Add(objMenuItem);
            }
            catch (Exception ex)
            {
                ex.Data.Add("My Key", "AddItem()--:--ctlMenu.xaml.cs--:--" + ex.Message + " :--:--");
                ClsException.LogError(ex);
                ClsException.WriteToErrorLogFile(ex);
            }
        }

        private void btnMenu_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                menuPopUP.IsOpen = (!menuPopUP.IsOpen);
            }
            catch (Exception ex)
            {
                ex.Data.Add("My Key", "btnMenu_Click()--:--ctlMenu.xaml.cs--:--" + ex.Message + " :--:--");
                ClsException.LogError(ex);
                ClsException.WriteToErrorLogFile(ex);
            }
        }

        private void btnClosePopUp_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                menuPopUP.IsOpen = (!menuPopUP.IsOpen);
            }
            catch (Exception ex)
            {
                ex.Data.Add("My Key", "btnClosePopUp_Click()--:--ctlMenu.xaml.cs--:--" + ex.Message + " :--:--");
                ClsException.LogError(ex);
                ClsException.WriteToErrorLogFile(ex);
            }
        }

        public bool AddBuddy(string strBuddyName)
        {
            try
            {
                return objEMIBuddyList.AddBuddy(strBuddyName);
            }
            catch (Exception ex)
            {
                ex.Data.Add("My Key", "AddBuddy()--:--ctlMenu.xaml.cs--:--" + ex.Message + " :--:--");
                ClsException.LogError(ex);
                ClsException.WriteToErrorLogFile(ex);
                return false;
            }
        }

        private void objMIRename_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            try
            {
                ((ctlPgTabHeader)((ContentPresenter)((Grid)this.Parent).FindName("conHeader")).Content).txtTitle_MouseDown(sender, e);
            }
            catch (Exception ex)
            {
                ex.Data.Add("My Key", "objMIRename_PreviewMouseLeftButtonDown()--:--ctlMenu.xaml.cs--:--" + ex.Message + " :--:--");
                ClsException.LogError(ex);
                ClsException.WriteToErrorLogFile(ex);
            }
        }

        private void objMISave_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            try
            {
                ((VMuktiGrid.ctlPage.TabItem)((Border)((Grid)this.Parent).Parent).TemplatedParent).Save();
                objMISave.IsEnabled = false;
            }
            catch (Exception ex)
            {
                ex.Data.Add("My Key", "objMISave_PreviewMouseLeftButtonDown()--:--ctlMenu.xaml.cs--:--" + ex.Message + " :--:--");
                ClsException.LogError(ex);
                ClsException.WriteToErrorLogFile(ex);
            }
        }

        private void objMIDelete_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            try
            {
                if (((Border)((Grid)this.Parent).Parent).TemplatedParent.GetType() == typeof(VMuktiGrid.ctlPage.TabItem))
                {
                    ((VMuktiGrid.ctlPage.TabItem)((Border)((Grid)this.Parent).Parent).TemplatedParent).Delete();
                }
                else if (((Border)((Grid)this.Parent).Parent).TemplatedParent.GetType() == typeof(VMuktiGrid.ctlTab.TabItem))
                {
                    ((VMuktiGrid.ctlTab.TabItem)((Border)((Grid)this.Parent).Parent).TemplatedParent).Delete();
                }
            }
            catch (Exception ex)
            {
                ex.Data.Add("My Key", "objMIDelete_PreviewMouseLeftButtonDown()--:--ctlMenu.xaml.cs--:--" + ex.Message + " :--:--");
                ClsException.LogError(ex);
                ClsException.WriteToErrorLogFile(ex);
            }
        }

        private void objMIClose_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            try
            {
                if (((Border)((Grid)this.Parent).Parent).TemplatedParent.GetType() == typeof(VMuktiGrid.ctlPage.TabItem))
                {
                    ((VMuktiGrid.ctlPage.TabItem)((Border)((Grid)this.Parent).Parent).TemplatedParent).Close();
                }
                else if (((Border)((Grid)this.Parent).Parent).TemplatedParent.GetType() == typeof(VMuktiGrid.ctlTab.TabItem))
                {
                    ((VMuktiGrid.ctlTab.TabItem)((Border)((Grid)this.Parent).Parent).TemplatedParent).Close();
                }
            }
            catch (Exception ex)
            {
                ex.Data.Add("My Key", "objMIClose_PreviewMouseLeftButtonDown()--:--ctlMenu.xaml.cs--:--" + ex.Message + " :--:--");
                ClsException.LogError(ex);
                ClsException.WriteToErrorLogFile(ex);
            }
        }
    }
}
